package com.nn.service.api;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.dnspod.v20210323.DnspodClient;
import com.tencentcloudapi.dnspod.v20210323.models.*;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;


@Component
public class DomainUtils {


    @Resource
    JavaMailSender sender;

    @Value("${id}")
    String id;

    @Value("${key}")
    String key;

    static String ip;

    public  void updateDomain(String ip){
        try{
            Credential cred = new Credential(id,key);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("dnspod.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            DnspodClient client = new DnspodClient(cred, "", clientProfile);
            ModifyRecordRequest req = new ModifyRecordRequest();
            req.setDomain("年年.online");
            req.setRecordType("AAAA");
            req.setRecordLine("默认");
            req.setValue(ip);
            req.setRecordId(1698790905L);
            ModifyRecordResponse resp = client.ModifyRecord(req);
        } catch (TencentCloudSDKException e) {
            System.out.println(e);
        }
    }

    public void ipChange(){
        Logger log = LoggerFactory.getILoggerFactory().getLogger("");
        URL url = null;
        try {
            url = new URL("http://6.ipw.cn");
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
                String ip = br.readLine();
                updateDomain(ip);
                if (DomainUtils.ip==null || !DomainUtils.ip.equals(ip) ){
                    log.info("IP地址已更新为："+ip);
                    sendEmail(ip);
                    DomainUtils.ip =ip;
                }
                br.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    public void sendEmail(String content){
          //SimpleMailMessage是一个比较简易的邮件封装，支持设置一些比较简单内容
        SimpleMailMessage message = new SimpleMailMessage();
          //设置邮件标题
        message.setSubject("域名ip更新通知");
          //设置邮件内容
        message.setText("域名ip更新为："+content);
          //设置邮件发送给谁，可以多个，这里就发给你的QQ邮箱
        message.setTo("gdh2286894971@163.com");
          //邮件发送者，这里要与配置文件中的保持一致
        message.setFrom("13123027918@163.com");
          //OK，万事俱备只欠发送
        sender.send(message);
    }



}
